<?php
try{
    include '../ini.php';
    include '../classes/xl_banner.php';
    $dt_xl_banner=new xl_bannner;
    if(empty($_GET['id'])){
        throw new Exception('Không tồn tại mã này');
    }
    $list_banner=$dt_xl_banner->check_id($_GET['id']);
    if($list_banner == NULL){
        throw new Exception('Không tài tại mã quảng cáo này');
    }
    if($_GET['id'] != $list_banner['id']){
        $_GET['id']=$dt_xl_admin->removeSQLI($_GET['id']);
        header("Location: banner_update.php?id=".$_GET['id']);
    }
    $location_banner=$dt_xl_banner->read_list();
    $read_products=$dt_xl_banner->read_product();
    $read_location=$dt_xl_banner->read_location();
    $dt_smarty->assign('list_banner',$list_banner);
    $dt_smarty->assign('read_location',$read_location);
    $dt_smarty->assign('location_banner',count($location_banner));
    $dt_smarty->assign('read_products',$read_products);
   	$contenForLayout=$dt_smarty->fetch('banner/banner_update.tpl');
	$dt_smarty->assign('contenForLayout',$contenForLayout);
	$dt_smarty->assign('titleForLayout','Chỉnh sửa quảng cáo');
	$dt_smarty->display('layout/defaults.tpl');
    $dbh=NULL;
}catch(Exception $e){
    $dbh=NULL;
    $_SESSION['msg']=$e->getMessage();
    $_SESSION['type_msg']='error';
    if(empty($_SERVER['HTTP_REFERER'])){
        header('Location: list_banner.php');
    } else {
        header('Location:'.$_SERVER['HTTP_REFERER']);
    }
}
function print_list($list,$id,$characters,$id_select){
        foreach($list as $location){
            if($location['parent']==$id){
                if($location['id'] == $id_select){
                    echo '<option selected="selected" value="'.$location['id'].'">'.$characters,$location['name'].'</option>';
                }else{
                    echo '<option value="'.$location['id'].'">'.$characters,$location['name'].'</option>';
                }
                print_list($list,$location['id'],$characters.$characters,$id_select); 
            }
        }  
}